package com.health.home.controller;
import com.health.common.core.utils.StringUtils;
import com.health.home.domain.po.TnewsPo;
import com.health.home.domain.vo.TnewVo;
import com.health.home.service.NewService;
import com.health.home.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xpz
 * @date 2024/12/16 10:30
 */
@RestController
@RequestMapping("/new")
public class TnewController {


    @Autowired
    private NewService newService;

    /**
     * 查询资讯信息表 对接后端管理
     * @param tnewVo
     * @return
     */
    @PostMapping("/newList")
    public Result<List<TnewsPo>> newList(@ModelAttribute TnewVo tnewVo){
        List<TnewsPo> tnewsPos = newService.newList(tnewVo);
        return Result.success(tnewsPos);
    }

    /**
     * 新增资讯信息
     */
    @PostMapping("/newAdd")
    public Result newAdd(@ModelAttribute TnewsPo tnewsPo){
        if (StringUtils.isBlank(tnewsPo.getNewsContent())){
            return Result.error("资讯内容不能为空");
        }
        if (StringUtils.isBlank(tnewsPo.getNewsTitle())){
            return Result.error("资讯标题不能为空");
        }
        newService.save(tnewsPo);
        return Result.success();
    }
    /**
     * 删除
     */
    @PostMapping("/newDel")
    public Result newDel(Integer id){
        newService.removeById(id);
        return Result.success();
    }
    /**
     * 修改
     */
    @PostMapping("/newUpdate")
    public Result newUpdate(@ModelAttribute TnewsPo tnewsPo){
        if (StringUtils.isNotBlank(tnewsPo.getNewsContent())){
            return Result.error("资讯内容不能为空");
        }
        if (StringUtils.isBlank(tnewsPo.getNewsTitle())){
            return Result.error("资讯标题不能为空");
        }
        newService.updateById(tnewsPo);
        return Result.success();
    }




}
